import * as api from "../../plugins/api.js"
import * as types from './mutation-types'
import _ from 'underscore'
export const session = async function ({
  commit
}) {
  try {
    let data = await api.session()
    if (_.indexOf(["Teacher", "Admin", "Agent"], data.usertype) == -1) {
      console.log(123)
      data = false
      return data
    } else {
      commit(types.SET_U, data)
      return true
    }
  } catch (err) {
    throw (err)
  }
}

export const login = async function ({
  commit
}, datas) {
  try {
    let data = await api.asynclogin(datas)
    commit(types.SET_U, data)
    return data
  } catch (err) {
    throw (err)
  }
}


export const actionCatebag = async function ({
  commit
}) {
  try {
    let data = await api.getAllCatebag()
    commit(types.SET_CATEBAG, data)
  } catch (err) {
    throw (err)
  }
}

export const actionCourse = async function ({
  commit
}) {
  try {
    let data = await api.getallcourse()
    commit(types.SET_COURSE, data)
  } catch (err) {
    throw (err)
  }
}

export const actionCoursecatebag = async function ({
  commit
}) {
  try {
    let data = await api.asyncgetcoursecatebag();
    commit(types.SET_COURSECATEBAG, data);
  } catch (err) {
    throw (err)
  }
}

export const actionSecretfile = async function ({
  commit
}) {
  try {
    let data = await api.asyncgetsecretfile({ order: "paixu" });
    commit(types.SET_SECRETFILE, data);
  } catch (err) {
    throw (err)
  }
}

export const actionTeacherproposal = async function ({
  commit
}) {
  try {
    let data = await api.asyncgetteacherproposal({ order: "paixu" });
    commit(types.SET_TEACHERPROPOSAL, data);
  } catch (err) {
    throw (err)
  }
}

export const actionTpdata = async function ({
  commit
}) {
  try {
    let data = await api.asyncgettpdata()
    commit(types.SET_TPDATA, data);
  } catch (err) {
    throw (err)
  }
}

export const actionItembank = async function ({
  commit
}) {
  try {
    let data = await api.asyncgetitembank({ order: "paixu" });
    commit(types.SET_ITEMBANK, data);
  } catch (err) {
    throw (err)
  }
}

export const actionItembankgroup = async function ({
  commit
}) {
  try {
    let data = await api.asyncgetitembankgroup({ order: "paixu" });
    commit(types.SET_ITEMBANKGROUP, data);
  } catch (err) {
    throw (err)
  }
}

export const actionItembankitembankgroup = async function ({
  commit
}) {
  try {
    let data = await api.asyncgetitembankitembankgroup();
    commit(types.SET_ITEMBANKITEMBANKGROUP, data);
  } catch (err) {
    throw (err)
  }
}

export const actionTeacherbyiditembank = async function ({
  commit
}) {
  try {
    let data = await api.asyncgetteacheritembank();
    commit(types.SET_TEACHERITEMBANKITEMBANK, data);
  } catch (err) {
    throw (err)
  }
}

export const actionAnswer = async function ({
  commit
}) {
  try {
    let data = await api.asyncgetanswerinteacher();
    commit(types.SET_ANSWER, data);
  } catch (err) {
    throw (err)
  }
}